package com.goodving.guli.bt;

import com.goodving.guli.bt.model.C3903a;
import com.goodving.guli.bt.model.C3904b;
import com.goodving.guli.bt.model.C3910h;
import com.goodving.guli.bt.profile.scale.C0638c;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.profile.scale.INotifyCallback;
import com.goodving.guli.bt.profile.scale.C3942j;
import com.goodving.guli.bt.profile.scale.GattUtils;
import com.goodving.guli.bt.profile.scale.HeartRateInfo;
import com.goodving.guli.view.C4220a;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by john on 2015/12/7.
 */
public class C3839z extends C1035B implements INotifyCallback {
    private boolean f19356A;
    private volatile boolean f19357B;
    public final byte f19358a;
    public final byte f19359b;
    boolean f19360c;
    boolean f19361d;
    int f19362e;
    int f19363f;
    int f19364g;
    int f19365h;
    int f19366i;
    Calendar f19367j;
    private final int f19368r;
    private final int f19369s;
    private final int f19370t;
    private Timer f19371u;
    private TimerTask f19372v;
    private int f19373w;
    private PipedInputStream f19374x;
    private PipedOutputStream f19375y;
    private final C3920r f19376z;

    public C3839z(C0638c c0638c) {
        super(c0638c);
        this.f19368r = C4220a.bT;
        this.f19369s = 5000;
        this.f19358a = (byte) 0;
        this.f19359b = (byte) 1;
        this.f19370t = 1;
        this.f19371u = null;
        this.f19372v = null;
        this.f19373w = -1;
        this.f19360c = false;
        this.f19361d = false;
        this.f19362e = -1;
        this.f19363f = -1;
        this.f19364g = -1;
        this.f19365h = 0;
        this.f19366i = 0;
        this.f19367j = null;
        this.f19374x = null;
        this.f19375y = null;
        this.f19376z = new C3920r();
        this.f19356A = false;
        this.f19357B = false;
    }

    public C3839z(C3942j c3942j, C0638c c0638c) {
        super(c3942j, c0638c);
        this.f19368r = C4220a.bT;
        this.f19369s = 5000;
        this.f19358a = (byte) 0;
        this.f19359b = (byte) 1;
        this.f19370t = 1;
        this.f19371u = null;
        this.f19372v = null;
        this.f19373w = -1;
        this.f19360c = false;
        this.f19361d = false;
        this.f19362e = -1;
        this.f19363f = -1;
        this.f19364g = -1;
        this.f19365h = 0;
        this.f19366i = 0;
        this.f19367j = null;
        this.f19374x = null;
        this.f19375y = null;
        this.f19376z = new C3920r();
        this.f19356A = false;
        this.f19357B = false;
    }

    class C3811A extends TimerTask {
        final /* synthetic */ C3839z f19305a;

        C3811A(C3839z c3839z) {
            this.f19305a = c3839z;
        }

        public void run() {
            GuliLog.m12370d(C1035B.f5062p, "run stopGetActivities!!!");
            this.f19305a.f19357B = true;
            this.f19305a.m25084j();
        }
    }

    private void m25078a(int i) {
        this.f19371u = new Timer();
        this.f19372v = new C3811A(this);
        this.f19371u.schedule(this.f19372v, (long) i);
    }

    private void m25079a(int i, int i2, Calendar calendar) {
        int i3 = 4;
        this.f19361d = true;
        this.f19365h = i;
        this.f19366i = 0;
        this.f19367j = calendar;
        GuliLog.m12370d("xxx", "currentDataLen: " + this.f19366i + ",currentDataTotalLen:" + this.f19365h);
        GuliLog.m12370d("xxx", "timestamp: " + this.f19367j.getTime().toString());
        GuliLog.m12370d("xxx", "totalLen: " + (i2 / (this.f19356A ? 4 : 3)) + " minute(s)");
        String str = "xxx";
        StringBuilder append = new StringBuilder().append("current: ");
        int i4 = this.f19365h;
        if (!this.f19356A) {
            i3 = 3;
        }
        GuliLog.m12370d(str, append.append(i4 / i3).append(" minute(s)").toString());
    }

    private boolean m25082b(int i) {
        if (this.f19361d) {
            this.f19366i += i;
            GuliLog.m12370d("xxx", "currentDataLen: " + this.f19366i + ",currentDataTotalLen:" + this.f19365h);
            if (i == 20 || this.f19366i == this.f19365h) {
                return true;
            }
            GuliLog.m12370d("xxx", "package lenght != 20 and current currentDataLen != currentDataTotalLen!!!");
            C0668a.m8144c(C0668a.eq);
            m25094d();
            return false;
        }
        GuliLog.m12370d("xxx", "get data before get head!!!");
        C0668a.m8144c(C0668a.ep);
        m25094d();
        return false;
    }

    private void m25083i() {
        if (this.f19371u != null) {
            this.f19371u.cancel();
            this.f19371u.purge();
            this.f19371u = null;
        }
        if (this.f19372v != null) {
            this.f19372v.cancel();
            this.f19372v = null;
        }
    }

    private void m25084j() {
        GuliLog.m12370d(f5062p, "stopGetActivities");
        this.f5064q.m25620z();
        try {
            if (this.f19374x != null) {
                this.f19374x.close();
                this.f19374x = null;
            }
            if (this.f19375y != null) {
                this.f19375y.close();
                this.f19375y = null;
            }
        } catch (Exception e) {
        }
    }

    private C3904b m25085k() {
        int i;
        int i2 = 4;
        GuliLog.m12371e();
        try {
            byte read = (byte) this.f19374x.read();
            Calendar gregorianCalendar = new GregorianCalendar(this.f19374x.read() + C4220a.bZ, this.f19374x.read(), this.f19374x.read(), this.f19374x.read(), this.f19374x.read(), this.f19374x.read());
            GuliLog.m12350a("timestamp: " + DateFormat.getDateTimeInstance().format(gregorianCalendar.getTime()));
            int read2 = ((this.f19374x.read() & HeartRateInfo.HR_EMPTY_VALUE) << 8) | (this.f19374x.read() & HeartRateInfo.HR_EMPTY_VALUE);
            GuliLog.m12350a("totalLen: " + read2 + " minute(s)");
            if (read == (byte) 1) {
                i = (this.f19356A ? 4 : 3) * read2;
            } else {
                i = read2;
            }
            this.f19376z.f19739a = i;
            i = (this.f19374x.read() & HeartRateInfo.HR_EMPTY_VALUE) | ((this.f19374x.read() & HeartRateInfo.HR_EMPTY_VALUE) << 8);
            GuliLog.m12350a("len: " + i + " minute(s)");
            if (read == (byte) 1) {
                if (!this.f19356A) {
                    i2 = 3;
                }
                i *= i2;
            }

            List arrayList = new ArrayList(i / 3);
            while (i > 0) {
                try {
                    byte read3 = (byte) this.f19374x.read();
                    byte read4 = (byte) this.f19374x.read();
                    byte read5 = (byte) this.f19374x.read();
                    C3920r c3920r;
                    if (this.f19356A) {
                        arrayList.add(new C3903a(read4, read5, read3, (byte) this.f19374x.read()));
                        i -= 4;
                        c3920r = this.f19376z;
                        c3920r.f19740b += 4;
                    } else {
                        arrayList.add(new C3903a(read4, read5, read3));
                        i -= 3;
                        c3920r = this.f19376z;
                        c3920r.f19740b += 3;
                    }
                    m25088a(this.f19376z);
                } catch (IOException e) {
                    //TBD
                }

            }
            return new C3904b(gregorianCalendar, arrayList);
        } catch (IOException e) {
            //TBD
        }
        return null;
    }

    public void m25086a() {
        GuliLog.m12370d(f5062p, "getActivities onStart");
    }

    public void m9823a(C0638c c0638c) {
        GuliLog.m12370d(f5062p, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        GuliLog.m12370d(f5062p, "===========================start sync data================================");
        GuliLog.m12370d(f5062p, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

        if (c0638c != null) {
            c0638c.m7910a();
        }
        if (this.f5064q != null) {
            C3910h B = this.f5064q.m25562B();
            if (B != null) {
                this.f19356A = B.m25440a();
                Object obj = null;
                int i = 0;
                while (i <= 1) {
                    this.f19357B = false;
                    obj = m25095e();
                    if (!this.f19357B) {
                        break;
                    }
                    i++;
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {
                    }
                }
                if (obj == null) {
                    GuliLog.m12370d(f5062p, "getActivities return null, now finish!");
                }
                if (c0638c != null) {
                    c0638c.m7912a(obj);
                }
                if (obj != null && ((List)(obj)).size() > 0) {
                    for (C3904b c3904b : ((List<C3904b>)(obj))) {
                        if (c3904b.f19684b != null) {
                            i = (this.f19356A ? 4 : 3) * c3904b.f19684b.size();
                        } else {
                            i = 0;
                        }
                        this.f5064q.m25598a(c3904b.f19683a, i);
                    }
                }
                GuliLog.m12370d(f5062p, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
                GuliLog.m12370d(f5062p, "============================stop sync data================================");
                GuliLog.m12370d(f5062p, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
                return;
            }
        }
        GuliLog.m12370d(f5062p, "Can not obtain GattPeripheral or deviceinfo. BleSyncDataTask task stopped");
    }

    public void m25088a(C3920r c3920r) {
        GuliLog.tracePrintInfo(f5062p, "getActivities onProgress:" + c3920r);
        int i = (c3920r.f19740b * 100) / c3920r.f19739a;
        if (i != this.f19373w) {
            this.f5063o.m7911a(i);
            this.f19373w = i;
        }
    }

    public void m25089a(String str) {
        GuliLog.m12370d(f5062p, "getActivities onError:" + str);
    }

    public synchronized void m25090a(boolean z) {
        GuliLog.m12370d(f5062p, "getActivities onBleNotify:" + z);
        m25083i();
        if (!z) {
            m25078a((int) 5000);
        }
    }

    public void notify(byte[] bArr) {
        try {
            boolean z;
            boolean z2;
            if (bArr.length == 11) {
                int i;
                byte b = bArr[0];
                int i2 = bArr[1] + C4220a.bZ;
                byte b2 = bArr[2];
                byte b3 = bArr[3];
                byte b4 = bArr[4];
                byte b5 = bArr[5];
                byte b6 = bArr[6];
                int i3 = (bArr[7] & HeartRateInfo.HR_EMPTY_VALUE) | ((bArr[8] & HeartRateInfo.HR_EMPTY_VALUE) << 8);
                int i4 = (bArr[9] & HeartRateInfo.HR_EMPTY_VALUE) | ((bArr[10] & HeartRateInfo.HR_EMPTY_VALUE) << 8);
                if (b == 1) {
                    i = (this.f19356A ? 4 : 3) * i3;
                    i4 = (this.f19356A ? 4 : 3) * i4;
                } else {
                    i = i3;
                }
                Calendar gregorianCalendar = new GregorianCalendar(i2, b2, b3, b4, b5, b6);
                if (!this.f19360c) {
                    this.f19360c = true;
                    this.f19362e = b;
                    this.f19363f = i;
                    this.f19364g = i2;
                    z = i4 == 0;
                    m25079a(i4, i, gregorianCalendar);
                    z2 = true;
                } else if (b != this.f19362e || i != this.f19363f) {
                    z2 = m25082b(bArr.length);
                    z = false;
                } else if (this.f19366i == this.f19365h) {
                    z = i4 == 0;
                    m25079a(i4, i, gregorianCalendar);
                    z2 = true;
                } else {
                    GuliLog.m12370d("xxx", "get data not complete but head coming!!!");
                    C0668a.m8144c(C0668a.er);
                    m25094d();
                    z2 = false;
                    z = false;
                }
            } else {
                z2 = m25082b(bArr.length);
                z = false;
            }
            GuliLog.m12370d(f5062p, GattUtils.m25335b(bArr));
            if (z2) {
                this.f19375y.write(bArr);
                m25090a(z);
                return;
            }
            m25090a(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public synchronized void m25092b() {
        GuliLog.m12370d(f5062p, "getActivities onStop");
        m25083i();
        this.f5063o.m7911a(100);
    }

    public synchronized void m25093c() {
        GuliLog.m12370d(f5062p, "getActivities onCommand");
        m25083i();
        m25078a((int) C4220a.bT);
    }

    public synchronized void m25094d() {
        GuliLog.m12370d(f5062p, "getActivities onMissData");
        this.f19357B = true;
        m25083i();
        m25084j();
    }

    public List<C3904b> m25095e() {
        GuliLog.m12370d(f5062p, "getActivities");
        m25086a();
        List<C3904b> arrayList = new ArrayList();
        try {
            this.f19374x = new PipedInputStream();
            this.f19375y = new PipedOutputStream();
            this.f19374x.connect(this.f19375y);
            this.f5064q.m25584a((INotifyCallback) this);
            boolean U = this.f5064q.m25581U();
            GuliLog.ASSERTION_TRUE(U);
            if (U) {
                m25093c();
                this.f19376z.f19739a = -1;
                this.f19376z.f19740b = 0;
                C3904b k = null;
                do {
                    try {
                        k = m25085k();
                        arrayList.add(k);
                    } catch (Exception e) {
                        m25089a(e.getMessage());
                    }
                } while (k.f19684b.size() != 0);
                m25092b();
                this.f5064q.m25582V();
                try {
                    if (this.f19374x != null) {
                        this.f19374x.close();
                        this.f19374x = null;
                    }
                    if (this.f19375y != null) {
                        this.f19375y.close();
                        this.f19375y = null;
                    }
                } catch (Exception e2) {
                }
                return arrayList;
            }
            m25089a("Write sync command failed!!!");
            m25092b();
            this.f5064q.m25582V();
            return null;
        } catch (IOException e3) {
            m25089a(e3.getMessage());
            m25092b();
            e3.printStackTrace();
            return null;
        }
    }
}
